package com.gaotai.wangfei.exam.system.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gaotai.wangfei.exam.filter.config.InterceptorChain;
import com.gaotai.wangfei.exam.ioc.service.BeanFactory;
import com.gaotai.wangfei.exam.ioc.service.bean.ClassPathXmlApplicationContext;
import com.gaotai.wangfei.exam.system.domain.StudentDomain;

/**
 * 
 * <p>
 * Description: [.do请求的总代理类]
 * </p>
 * 
 * @author <a href="mailto: wangfei@gaotai.cn">王飞</a>
 * @version $Revision$
 */
public class MainAgentServlet extends HttpServlet
{
	//容器中的bean工厂
	private BeanFactory beanFactory;

	/**
	 * <p>
	 * [字段功能描述]
	 * </p>
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public MainAgentServlet()
	{
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy()
	{
		super.destroy(); // Just puts "destroy" string in log
	}

	/**
	 * 
	 * <p>
	 * 因处理逻辑一致，所以将get请求全部转到post请求处理
	 * </p>
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @author:wangfei 2016-3-17
	 * @update: [updatedate] [changer][change description]
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		doPost(request, response);
	}

	/**
	 * 
	 * <p>
	 * 对于请求做分派转发且经过拦截器链
	 * </p>
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 * @author:wangfei 2016-3-17
	 * @update: [updatedate] [changer][change description]
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		//获得容器中wangfei的bean
		StudentDomain wangFeiDomain = (StudentDomain) beanFactory.getBean("wangfei");
		request.setAttribute("wangFeiDomain", wangFeiDomain);//存值
		StudentDomain luXiaoXiangDomain = (StudentDomain) beanFactory.getBean("luxiaoxiang");
		request.setAttribute("luXiaoXiangDomain", luXiaoXiangDomain);//存值
		request.setAttribute("wang_city", "香港");
		request.setAttribute("lu_city", "北京");
		//获得容器中配置的拦截器链
		InterceptorChain interceptorChain = (InterceptorChain) beanFactory.getBean("interceptor");
		interceptorChain.doFilter(request, response, interceptorChain);
		//解析分派请求的servletBean的名称
		String requestUri = request.getRequestURI();
		String servletBeanName = requestUri.substring(request.getContextPath().length() + 1, requestUri.indexOf(".do"));
		Servlet servletBean = (HttpServlet) beanFactory.getBean(servletBeanName);
		//交由相关的servletBean去处理请求
		if(servletBean != null)
		{
			servletBean.service(request, response);
		}
	}

	/**
	 * 
	 * <p>
	 * 启动的时候初始IOC化容器
	 * </p>
	 * 
	 * @throws ServletException
	 * @author:wangfei 2016-3-17
	 * @update: [updatedate] [changer][change description]
	 */
	public void init() throws ServletException
	{
		beanFactory = new ClassPathXmlApplicationContext("/applicationContext.xml");
	}

}
